package com.xiaobawang.homework1225.mapper;

import com.xiaobawang.homework1225.entity.User;
import com.xiaobawang.homework1225.entity.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * description：User Mapper
 * author：Synhard
 * create time：2019/12/25 09：34
 * last modified: 2019/12/25 09：34
 * **/

@Mapper
@Repository
public interface UserMapper {

    @Insert("INSERT INTO user(u_name, u_pwd, u_email, u_create_time) VALUES (#{u_name}, #{u_pwd}, #{u_email}, #{u_create_time})")
    int insertUser (String u_name, String u_pwd, String u_email, Date u_create_time);

    @Delete("DELETE FROM user WHERE u_id = #{u_id}")
    int deleteUser(int u_id);

    @Update("UPDATE user SET u_name = #{u_name}, u_pwd = #{u_pwd}, u_email = #{u_email}, u_create_time = #{u_create_time} WHERE u_id = #{u_id}")
    int alterUser(User user);

    @Select("SELECT * FROM user")
    List<User> selectUser();

    @Select("SELECT * FROM user WHERE u_id = #{u_id}")
    User selectUserById(int u_id);

    @Select("SELECT * FROM user WHERE u_name = #{u_name}")
    User selectUserByName(String u_name);

    @Select("SELECT * FROM user WHERE u_name like '%${u_name}%'")
    List<User> fuzzySelectUserByName(String u_name);

    @Select("SELECT * FROM user WHERE u_create_time BETWEEN #{begin} AND #{end}")
    List<User> selectUserByTime(Date begin, Date end);

    @Select("SELECT user.u_name, user.u_email, user.u_create_time, role.r_name FROM user INNER JOIN userrolerelation ON (user.u_id = userrolerelation.user_id) INNER JOIN role ON (userrolerelation.role_id = role.r_id)")
    List<UserInfo> selectUserInfo();
}
